package com.sinodata.bsm.cicp.collector.jboss;

import java.util.ArrayList;
import java.util.List;

import javax.management.ObjectName;

import org.apache.log4j.Logger;
import org.jboss.jmx.adaptor.rmi.RMIAdaptor;

import com.sinodata.bsm.cicp.collector.Collector;
import com.sinodata.bsm.cicp.exception.CollectorException;
import com.sinodata.bsm.cicp.util.RmiUtil;
import com.sinodata.bsm.common.vo.ResPropertyValue;

/**
 * 
 * <p>
 * Description: Jboss线程池各项指标的采集
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-7-24 下午09:52:59          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class ThreadpoolCollector extends Collector {
    private static Logger logger = Logger.getLogger(ThreadpoolCollector.class);

    @Override
    public List<ResPropertyValue> collect(String ip) throws CollectorException {
        List<ResPropertyValue> values = new ArrayList<ResPropertyValue>();
        String[] propMetas = getPropMeta();
        ResPropertyValue propertyValueX = new ResPropertyValue();
        ResPropertyValue.PropValue[] propValues = new ResPropertyValue.PropValue[propMetas.length];
        try {
            //            RMIAdaptor rmiAdapter = RmiUtil.getRmiAdaptor(ip, Integer.parseInt(this.getParamMap().get("端口")));
            RMIAdaptor rmiAdapter = RmiUtil.getRmiAdaptor(ip, Integer.parseInt(this.getParamMap().get("Port")));
            ObjectName jbossThreadPool = new ObjectName("jboss.system:service=ThreadPool");

            String PoolNumber = rmiAdapter.getAttribute(jbossThreadPool, "PoolNumber") + "";
            String MinimumPoolSize = rmiAdapter.getAttribute(jbossThreadPool, "MinimumPoolSize") + "";
            String MaximumPoolSize = rmiAdapter.getAttribute(jbossThreadPool, "MaximumPoolSize") + "";
            String MaximumQueueSize = rmiAdapter.getAttribute(jbossThreadPool, "MaximumQueueSize") + "";
            String QueueSize = rmiAdapter.getAttribute(jbossThreadPool, "QueueSize") + "";

            propValues[0] = ResPropertyValue.createPropValue(PoolNumber, propMetas[0]);
            propValues[1] = ResPropertyValue.createPropValue(MinimumPoolSize, propMetas[1]);
            propValues[2] = ResPropertyValue.createPropValue(MaximumPoolSize, propMetas[2]);
            propValues[3] = ResPropertyValue.createPropValue(MaximumQueueSize, propMetas[3]);
            propValues[4] = ResPropertyValue.createPropValue(QueueSize, propMetas[4]);

            propertyValueX.setValues(propValues);
            values.add(propertyValueX);
            return values;
        } catch (Exception e) {
            logger.error(e);
            throw new CollectorException(e);
        }
    }

    @Override
    public String[] getParamMeta() {
        //        return new String[] { "端口", "名称" };
        return new String[] { "Port", "Name" };
    }

    @Override
    public String[] getPropMeta() {
        //        return new String[] { "池化线程数量", "最小池大小", "最大池大小", "最大队列长度", "处理队列长度" };
        //        return new String[] { "ProcessesNumInProcessPool", "MinPoolNum", "MaxPoolNum", "MaxQueueLength", "HandleQueueLength" };
        return new String[] { "$1", "$2", "$3", "$4", "$5", "$6" };
    }
}
